<template>
  <!-- 个人史 -->
  <el-form class="basic_form">
    <el-row>
      <el-col :span="24">
        <el-form-item label="过敏史：">
          <el-radio-group v-model="person_form.isAllergic">
            <el-radio :label="1">有</el-radio>
            <el-radio :label="2">否认</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-col>
    </el-row>
    <!-- <allergic
      v-if="person_form.isAllergic==1"
      :itemList="itemList"
      @addItem="addItem"
      @removeItem="removeItem"
    ></allergic> -->
    <!-- <allergic
      v-if="person_form.isAllergic==1"
      :itemList="itemList"
      @addItem="addItem"
      @removeItem="removeItem"
    ></allergic> -->
    <guoMinShi v-if="person_form.isAllergic==1" v-model="patientAllergyArr" :isAdd="true" @onAdd="onAddGuoMin"></guoMinShi>
    <!-- <el-row v-if="person_form.isAllergic==1 && itemList.length==0">
      <el-button  @click="addItem()">
           添加过敏源
          <i class="el-icon-plus"></i>
        </el-button>
    </el-row> -->
    <el-row>
      <el-col :span="24">
        <el-form-item label="个人史：">
          <el-input type="textarea" v-model="person_form.personalHistory"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item label="家族史：">
          <el-input type="textarea" v-model="person_form.familyHistory"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item label="既往史：">
          <el-input type="textarea" v-model="person_form.pastHistory"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item label="疫苗接种史：">
          <el-input type="textarea" v-model="person_form.seedlingsHistory"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item label="	经带胎产史：">
          <el-input type="textarea" v-model="person_form.afterTheBirth"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <!-- <el-row v-if="detail.age<18">
      <el-col :span="24">
        <el-form-item label="喂养史：">
          <el-input type="textarea" v-model="person_form.familyHistory"></el-input>
        </el-form-item>
      </el-col>
    </el-row> -->
    <el-form-item label prop>
      <el-button type="primary" @click="onSubmit">保存</el-button>
      <el-button @click="cancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
export default {
  props: ["patientId", "detail","personForm", "item"],
  data() {
    return {
      person_form: {
        isAllergic: 2, //是否过敏
        personalHistory: "", //个人史
        seedlingsHistory:"",//疫苗接种史
        familyHistory: "", //家族史
        pastHistory:'',//既往史
        afterTheBirth:'',//经带胎产史
        isDeleted: 0 //0有效1删除
      },
      patientAllergyArr: [],
      itemList: [
        {
          allergens: "", //过敏源
          allergicReaction: "", //过敏反应
          allergicDegree: "" //过敏程度
        }
      ]
    };
  },
  mounted() {
    let th = this;
    th.getPatientHistoryById();
  },
  methods: {
    onSubmit() {
      let th = this;
      let j = {};
      let json = {};
      Object.assign(j, th.person_form);
      j.patientId = th.patientId;
      json.patientId = th.patientId;
      if(th.patientAllergyArr.length>0){
        for(let i=0;i<th.patientAllergyArr.length;i++){
          th.patientAllergyArr[i].patientId=th.patientId
        }
        if(th.person_form.isAllergic==1){
          json.patientAllergy = JSON.stringify(th.patientAllergyArr);
        }else{
          json.patientAllergy=JSON.stringify([{patientId:th.patientId,allergicDegree:'',allergicReaction:'',allergens:''}]);
        }
      }else{
        json.patientAllergy=JSON.stringify([{patientId:th.patientId,allergicDegree:'',allergicReaction:'',allergens:''}]);
      }
      th.$api.patientHistorySaveHistory(j).then(res => {
        if (res.executed) {
          json.personalHistory=th.person_form.personalHistory
          json.familyHistory=th.person_form.familyHistory
          json.isAllergic=th.person_form.isAllergic
           th.$api.saveAllergyAndHistory(json).then(res => {
              if (res.executed) {
                th.$message.success("保存成功");
                th.$emit("success");
                th.$emit("get");
              } else {
                th.$message.error(res.message);
              }
           });
          // if (json.patientAllergy.length > 0) {
           
          // } else {
          //   th.$message.success(res.message);
          //   th.$emit("success");
          //   th.$emit("get");
          // }
        }
      });
    },
    addItem() {
      let ar = {
        allergens: "", //过敏源
        allergicReaction: "", //过敏反应
        allergicDegree: "" //过敏程度
      };
      this.itemList.push(ar);
    },
    removeItem(index) {
      this.itemList.splice(index, 1);
    },
    cancel() {
      this.$emit("cancel");
    },
    getPatientHistoryById() {
      let th = this;
      if (th.personForm) {
        let j={}
        th.person_form =Object.assign(j,th.personForm);
      }
      if (th.item) {
        th.itemList = th.item;
        th.patientAllergyArr=th.item;
      }
    },
    onAddGuoMin() {
      let th = this;
      let j = {
        allergens: "",
        allergicReaction: "",
        allergicDegree: "",
        patientId: th.patientId
      };
      th.patientAllergyArr.push(j);
    },
  },
  components: {
    // allergic: () => import("../../_com/allergicTmp")
     guoMinShi: () => import("@/pages/_com/guoMinShi.vue")
  }
};
</script>
<style lang="scss" scoped>
.basic_form {
  border-left: 1px solid #eee;
  border-right: 1px solid #eee;
  border-bottom: 1px solid #eee;
  padding: 10px;
}
</style>

